package com.oocl.admin_platform.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.oocl.admin_platform.pojo.Merchant;
import com.oocl.admin_platform.service.MerchantService;
import com.oocl.admin_platform.service.factory.ServiceFactory;



/**
 * Servlet implementation class GetMerchantListServlet
 */
public class GetMerchantListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private MerchantService merchantService;
	
	
	private Logger logger=Logger.getLogger(GetMerchantListServlet.class);
	
	
	
	
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetMerchantListServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		merchantService=ServiceFactory.getMerchantServiceInstance();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String action=request.getParameter("action");
		if ("all".equals(action)) {
			logger.info("search all merchant list");
			List<Merchant>merchantAllList=merchantService.findAll();
			request.getSession().setAttribute("merchantAllList",merchantAllList);
			logger.info("merchatAllList have already to the showMerchatAllList.jsp");
			response.sendRedirect("showAllList.jsp");
		}
		if ("search".equals(action)) {
			String status=request.getParameter("status");
			List<Merchant>merchantListWithStatus=merchantService.findByStatus(Integer.parseInt(status));
			request.getSession().setAttribute("status", status);
			request.getSession().setAttribute("merchantListWithStatus", merchantListWithStatus);
//			response.getWriter().write("showMerchantByStatusList.jsp");
			response.sendRedirect("showByStatusList.jsp");
		}
		
	}

}
